package com.itwlj.controller.user;

import com.itwlj.dto.OrderDTO;
import com.itwlj.result.Result;
import com.itwlj.service.OrderService;
import com.itwlj.vo.OrdersPreviewVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 订单
 */
@RestController("userOrderController")
@RequestMapping("/order")
@Api(tags = "C端订单相关接口")
@Slf4j
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 获取地址和商品信息
     * @return
     */
    @GetMapping("/pre")
    @ApiOperation("获取地址和商品信息")
    public Result<OrdersPreviewVO> getOrdersPreview(){
        log.info("获取地址和商品信息");
        OrdersPreviewVO ordersPreview = orderService.getOrdersPreview();
        return  Result.success(ordersPreview);
    }

    /**
     * 生成订单
     * @return
     */
    @PostMapping
    @ApiOperation("生成订单")
    public Result<Map<String,Long>> insertOrder(@RequestBody OrderDTO orderDTO){
        log.info("生成订单{}：", orderDTO);
        Long orderId = orderService.insert(orderDTO);

        Map<String,Long> orderIdMap = new HashMap<>();
        orderIdMap.put("id",orderId);
        return Result.success(orderIdMap);
    }
}
